#include "mvc/view/geometries_widget.h"
#include "ui_geometries_widget.h"
#include "mvc/model/project.h"
#include "common/uav_texture.h"

#include <iostream>
#include "string"
#include "sstream"

/**
  * The Geometries_widget constructor
  * parent - the widget parent
  */
Geometries_widget::Geometries_widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Geometries_widget)
{
    ui->setupUi(this);
    closed = false;
    connect(ui->tree_widget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
            SLOT(show_configuration(QTreeWidgetItem *)));
    connect(ui->simulation_button, SIGNAL(clicked()), this, SLOT(simulate()));
    drawed_configuration = -1;
    configuration_type = -1;
}

Geometries_widget::~Geometries_widget()
{
    delete ui;
    closed = true;
}

/**
  * Create a new configuration tree item.
  */
void Geometries_widget::create_configuration(int id_configuration, Parameters *values)
{
    QString cnumber(tr(""));
    if(id_configuration < 10)
        cnumber.append("00");
    else if(id_configuration > 9 && id_configuration < 100)
        cnumber.append("0");

    cnumber.append("%1");

    QString config_name = QString(PREFIX).append(cnumber).arg(id_configuration++);

    QTreeWidgetItem *config_item = new QTreeWidgetItem(ui->tree_widget);

    config_item->setText(0, config_name);

    QList<QString> keys = values->keys();

    for(int i = 0; i < values->size(); i++)
    {
        QString section_name = keys.at(i);
        QMap<QString, float> c_section = (*values)[section_name];

        add_item(config_item, i, section_name, c_section);
    }
}

/**
  * Draw the configuration
  * config - the configuration to draw
  */
void Geometries_widget::draw_configuration(const Configuration *config, bool draw_sim, int step)
{
    //cout << "TODO: draw configuration" << endl;
    ui->opengl_widget->draw_config(config, draw_sim, step);
}

/**
  * Set the configurations type of uav
  */
void Geometries_widget::set_configuration_type(int type)
{
    if(configuration_type == -1)
        configuration_type = type;
}

/**
  * Adds a new texture to opengl widget
  */
int Geometries_widget::add_texture(ITexture_image *texture)
{
    return ui->opengl_widget->add_texture(texture);
}

/**
 * @brief Geometries_widget_uav::getClosed
 * @return the value of atribute 'closed'
 */

bool Geometries_widget::getClosed(){
    return closed;
}

/**
  * Set text in the widget.
  */
void Geometries_widget::retranslateUi(){
    ui->retranslateUi(this);
}

//
// PUBLIC SLOTS
//

/**
  * Emit the draw signal to the View Component
  */
void Geometries_widget::show_configuration(QTreeWidgetItem *item)
{
    int to_draw_configuration;
    QString s;

    s = item->text(0).remove(0, QString(PREFIX).size());
    to_draw_configuration = s.toInt();

    if(drawed_configuration != to_draw_configuration)
    {
        drawed_configuration = to_draw_configuration;
        ui->simulation_button->setEnabled(true);
        emit s_draw_configuration(to_draw_configuration);
    }
}

/**
  * Emit the simulate signal to the view component.
  */
void Geometries_widget::simulate()
{
    emit s_simulate_configuration(drawed_configuration);
}

/**
  * Actions when right rotation is pressed
  */
void Geometries_widget::rrotate()
{
    ui->opengl_widget->r_rotate();
}

/**
  * Actions when left rotation is pressed
  */
void Geometries_widget::lrotate()
{
    ui->opengl_widget->l_rotate();
}

/**
  * Actions when up rotation is pressed
  */
void Geometries_widget::urotate()
{
    ui->opengl_widget->u_rotate();
}

/**
  * Actions when down rotation is pressed
  */
void Geometries_widget::drotate()
{
    ui->opengl_widget->d_rotate();
}

/**
  * Actions when zoom in is pressed
  */
void Geometries_widget::zin()
{
    ui->opengl_widget->zoom_in();
}

/**
  * Actions when zoom out is pressed
  */
void Geometries_widget::zout()
{
    ui->opengl_widget->zoom_out();
}

/**
  * Actions when right move is pressed
  */
void Geometries_widget::rmove()
{
    ui->opengl_widget->r_move();
}

/**
  * Actions when left move is pressed
  */
void Geometries_widget::lmove()
{
    ui->opengl_widget->l_move();
}

/**
  * Actions when up move is pressed
  */
void Geometries_widget::umove()
{
    ui->opengl_widget->u_move();
}

/**
  * Actions when down move is pressed
  */
void Geometries_widget::dmove()
{
    ui->opengl_widget->d_move();
}

/**
  * Actions when reset camera is pressed
  */
void Geometries_widget::reset_camera()
{
    ui->opengl_widget->reset_camera();
}

/**
  *  Actions when uav texture is selected
  */
void Geometries_widget::uavtex()
{
    ui->opengl_widget->enable_uavtexture();
}

/**
  *  Actions when simulation texture is selected
  */
void Geometries_widget::simtex()
{
    ui->opengl_widget->enable_simtexture();
}

/**
  *  Actions when perspective view is selected
  */
void Geometries_widget::perspective()
{
    ui->opengl_widget->show_perspective();
}

/**
  *  Actions when orthogonal view is selected
  */
void Geometries_widget::ortho()
{
    ui->opengl_widget->show_ortho();
}

//
// PRIVATE METHODS
//

/**
  * Add the uavs parts items to the configurations tree.
  */
void Geometries_widget::add_item(QTreeWidgetItem *parent, int pos, const QString item_name, QMap<QString, float> items)
{
    new QTreeWidgetItem(parent);
    QTreeWidgetItem *current_item = parent->child(pos);
    current_item->setText(0, item_name);

    QList<QString> c_keys = items.keys();
    for(int i = 0; i < items.size(); i++)
    {
        QTreeWidgetItem *ci = new QTreeWidgetItem(current_item);
        //ci->setFlags(ci->flags() | Qt::ItemIsEditable);
        QString c_item = c_keys.at(i);

        ci->setText(0, c_item);
        parent->child(pos)->child(i)->setText(1, QString::number(items[c_item]));
    }
}
